package com.sisyphus.simple.activities;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sisyphus.simple.data.DataRepository;
import com.sisyphus.simple.data.network.model.User;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.core.NetTaskFactory;
import com.sisyphus.spinach.network.presenters.FormTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.utils.Form;
import com.sisyphus.spinach.network.views.Debounce;
import com.sisyphus.spinach.network.views.SisyphusButton;
import com.sisyphus.spinach.network.views.SisyphusToast;
import com.sisyphus.spinach.ui.configs.IActivityConfigProvider;
import com.sisyphus.simple.R;


import java.util.Arrays;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * 用户登录
 */
public class LoginActivity extends AppCompatActivity implements IActivityConfigProvider {

    @Override
    public int getLayoutResID() {
        return R.layout.activity_login;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 登录
        findViewById(R.id.button_login).setOnClickListener(v -> login(((TextView) v)));

        // 注册
        findViewById(R.id.button_register).setOnClickListener(v ->
                startActivity(new Intent(this, RegisterActivity.class)));

        // 帖子
        findViewById(R.id.button_post_list).setOnClickListener(v ->
                startActivity(new Intent(this, PostListActivity.class)));
    }

    private void login(TextView button) {
        EditText editEmail = findViewById(R.id.edit_email);
        EditText editPassword = findViewById(R.id.edit_password);
        TextView textUserInfo = findViewById(R.id.text_user_info);

        new FormTask.Builder<>()
                .form(
                        new Form.NotEmpty(editEmail, R.string.error_not_empty_email),
                        new Form.NotEmpty(editPassword, R.string.error_not_empty_password),
                        new Form.Length(editPassword, User.MIN_PASSWORD_LENGTH, R.string.error_min_length_password)
                )
                .view(new SisyphusToast<>(this))
                .netTask(
                        NetTaskFactory.get(
                                getLifecycle(),
                                new NetSingleTask<>(
                                        getLifecycle(),
                                        DataRepository.getInstance()
                                                .login(
                                                        editEmail.getText().toString(),
                                                        editPassword.getText().toString()
                                                )
                                ),
                                new SisyphusButton<>(
                                        button,
                                        "登录中...",
                                        new SisyphusToast<>(this,
                                                new INetTask.View<User>() {
                                                    @Override
                                                    public void success(User data) {
                                                        textUserInfo.setText(new Gson().toJson(data));
                                                    }
                                                }
                                        )
                                )
                        )
                )
                .build()
                .execute();
    }
}
